package com.etc.delivery.controller.admin;

import com.etc.delivery.dao.admin.AdminWaybillDao;
import com.etc.delivery.dto.WaybillDto;
import com.etc.delivery.entity.Warehouse;
import com.etc.delivery.service.admin.IAdminWarehouseService;
import com.etc.delivery.service.admin.IAdminWaybillService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/admin")
@Controller
public class AdminWarehouseController {

    @Resource
    private IAdminWarehouseService warehouseService;

    @Resource
    private AdminWaybillDao adminWaybillDao;

    @RequestMapping("/queryWarehouse")
    @ResponseBody
    public PageInfo<Warehouse> queryWarehouse(String name, String address, Integer pageNum, Integer pageSize) {

        return warehouseService.queryWarehouse(name, address, pageNum, pageSize);
    }

    @RequestMapping(value = "queryWarehouseName")
    @ResponseBody
    public List<Warehouse> queryWarehouseName() {

        return warehouseService.queryWarehouseName();
    }

    @RequestMapping("/queryDeletedWarehouse")
    @ResponseBody
    public List<Warehouse> queryDeletedWarehouse(String name, String address) {

        return warehouseService.queryDeletedWarehouse(name, address);
    }


    @RequestMapping(value = "/addWarehouse")
    @ResponseBody
    public Map<String, String> addWarehouse(String name, String address, String phone, String password) {
        Map<String, String> map = new HashMap<>();
        if (name.isEmpty() || address.isEmpty() || phone.isEmpty() || password.isEmpty()) {
            map.put("code", "0");
            map.put("msg", "请完整填写信息");
            return map;
        }
        List<Warehouse> list = warehouseService.queryWarehousePhone(phone);

        if (!list.isEmpty()) {
            map.put("code", "0");
            map.put("msg", "号码不可用");
            return map;
        }
        Warehouse warehouse = new Warehouse();
        warehouse.setName(name);
        warehouse.setAddress(address);
        warehouse.setPhone(phone);
        warehouse.setPassword(password);
        boolean flag = warehouseService.addWarehouse(warehouse);
        if (flag) {
            map.put("code", "1");
            map.put("msg", "添加成功");
        } else {
            map.put("code", "0");
            map.put("msg", "添加失败");
        }

        return map;
    }

    @RequestMapping(value = "deleteWarehouse")
    @ResponseBody
    public Map<String, String> deleteWarehouse(Integer id) {
        Map<String, String> map = new HashMap<>();
        List<WaybillDto> list = adminWaybillDao.queryWaybill("", 0, id);
        if (!list.isEmpty()) {
            map.put("code", "0");
            map.put("msg", "仓库内仍有运单，不能删除");
            return map;
        }


        boolean flag = warehouseService.deleteWarehouse(id);
        if (flag) {
            map.put("code", "1");
            map.put("msg", "删除成功");
        } else {
            map.put("code", "0");
            map.put("msg", "删除失败");
        }
        return map;
    }

    @RequestMapping(value = "resumeWarehouse")
    @ResponseBody
    public Map<String, String> resumeWarehouse(Integer id) {
        Map<String, String> map = new HashMap<>();
        boolean flag = warehouseService.resumeWarehouse(id);
        if (flag) {
            map.put("code", "1");
            map.put("msg", "恢复成功");
        } else {
            map.put("code", "0");
            map.put("msg", "恢复失败");
        }
        return map;
    }

    @RequestMapping(value = "writeOff")
    @ResponseBody
    public Map<String, String> writeOff(Integer id) {
        Map<String, String> map = new HashMap<>();
        boolean flag = warehouseService.writeOff(id);
        if (flag) {
            map.put("code", "1");
            map.put("msg", "注销成功");
        } else {
            map.put("code", "0");
            map.put("msg", "注销失败");
        }
        return map;
    }

    @RequestMapping(value = "updateWarehouse")
    @ResponseBody
    public Map<String, String> updateWarehouse(Integer id, String name, String phone, String address) {
        Map<String, String> map = new HashMap<>();
        List<Warehouse> list = warehouseService.queryWarehousePhone(phone);

        if (!list.isEmpty()) {
            if (list.get(0).getId() != id) {
                map.put("code", "0");
                map.put("msg", "号码不可用");
                return map;
            }

        }
        Warehouse warehouse = new Warehouse();
        warehouse.setId(id);
        warehouse.setName(name);
        warehouse.setPhone(phone);
        warehouse.setAddress(address);

        boolean flag = warehouseService.updateWarehouse(warehouse);

        if (flag) {
            map.put("code", "1");
            map.put("msg", "修改成功");
        } else {
            map.put("code", "0");
            map.put("msg", "修改失败");
        }
        return map;
    }

}
